package com.school.information.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.information.entity.BasicSchoolEntity;
import com.school.information.entity.SysUserEntity;
import com.school.information.enums.result.BasicResultEnum;
import com.school.information.qo.BasicSchoolQO;
import com.school.information.service.BasicSchoolService;
import com.school.information.service.SysUserService;
import com.school.information.utils.NumberUtil;
import com.school.information.utils.ResultUtil;
import com.school.information.vo.SchoolPageVo;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * (BasicSchool)表控制层
 *
 * @author ycc
 * @since 2023-11-07 14:44:46
 */
@RestController
@Slf4j
@RequestMapping("basic/school")
public class BasicSchoolController {
    /**
     * 服务对象
     */
    @Resource
    private BasicSchoolService basicSchoolService;
    @Resource
    private SysUserService userService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param basicSchool 查询实体
     * @return 所有数据
     */
    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @PostMapping("/pageList")
    public ResultUtil pageList(@RequestBody BasicSchoolQO schoolQO) {
        IPage<BasicSchoolEntity> page = basicSchoolService.pageList(schoolQO);
        if (CollectionUtil.isEmpty(page.getRecords())) {
            return ResultUtil.success(page);
        }
        Set<Long> userIds = page.getRecords().stream().map(BasicSchoolEntity::getUpdateUser).collect(Collectors.toSet());
        Map<Long, SysUserEntity> userMap = userService.getUserMap(new ArrayList<>(userIds));
        List<SchoolPageVo> list = new LinkedList<>();
        page.getRecords().forEach(item -> {
            SchoolPageVo schoolPageVo = new SchoolPageVo();
            BeanUtils.copyProperties(item, schoolPageVo);
            schoolPageVo.setUpdateBy(userMap.get(item.getUpdateUser()).getRealName());
            list.add(schoolPageVo);
        });
        IPage<SchoolPageVo> resultPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        resultPage.setRecords(list);
        return ResultUtil.success(resultPage);
    }

    /**
     * 自动生成学校代码
     *
     * @return
     */
    @GetMapping("/generate")
    public ResultUtil generateKey() {
        String newCode = basicSchoolService.getMaxCode();
        return ResultUtil.success(newCode);
    }

    /**
     * 新增数据
     *
     * @param basicSchool 实体对象
     * @return 新增结果
     */
    @PostMapping("/save")
    public ResultUtil save(@RequestBody @Valid BasicSchoolEntity basicSchool) {
        if (Objects.isNull(basicSchool)) {
            return ResultUtil.ERROR_ARG;
        }
        if (basicSchoolService.saveInfo(basicSchool)) {
            return ResultUtil.SUCCESS_NO_DATA;
        }
        return ResultUtil.error(BasicResultEnum.ADD_SCHOOL_FAIL);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/getById/{id}")
    public ResultUtil getById(@PathVariable("id") Long id) {
        if (NumberUtil.isNullOrLessEqual0(id)) {
            return ResultUtil.ERROR_ARG;
        }
        BasicSchoolEntity schoolEntity = basicSchoolService.getById(id);
        return ResultUtil.success(schoolEntity);
    }

    /**
     * 更新学校信息
     *
     * @param schoolEntity
     * @return
     */
    @PutMapping("/update")
    public ResultUtil update(@RequestBody @Valid BasicSchoolEntity schoolEntity) {
        if (Objects.isNull(schoolEntity) || NumberUtil.isNullOrLessEqual0(schoolEntity.getId())) {
            return ResultUtil.ERROR_ARG;
        }
        if (basicSchoolService.updateInfo(schoolEntity)) {
            return ResultUtil.SUCCESS_NO_DATA;
        }
        return ResultUtil.error(BasicResultEnum.UPDATE_SCHOOL_FAIL);
    }

    /**
     * 删除学校信息
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public ResultUtil delete(@PathVariable("ids") Long[] ids) {
        if (ArrayUtils.isEmpty(ids)) {
            return ResultUtil.ERROR_ARG;
        }
        if (basicSchoolService.removeByIds(Arrays.asList(ids))) {
            return ResultUtil.SUCCESS_NO_DATA;
        }
        log.error("##学校信息id={}删除失败", ids);
        return ResultUtil.error(BasicResultEnum.DEL_SCHOOL_FAIL);
    }

    /**
     * 查询所有学校信息
     *
     * @return
     */
    @GetMapping("/list")
    public ResultUtil findList() {
        List<BasicSchoolEntity> resultList = basicSchoolService.findList();
        return ResultUtil.success(resultList);
    }
}

